package cn.com.tomato.controller;

import cn.com.tomato.controller.domain.AjaxResult;
import cn.com.tomato.controller.domain.todoSet.SortVo;
import cn.com.tomato.controller.domain.todoSet.ToDoVo;
import cn.com.tomato.controller.domain.todoSet.TodoSetVo;
import cn.com.tomato.model.Focus;
import cn.com.tomato.model.ToDo;
import cn.com.tomato.model.TodoSet;
import cn.com.tomato.service.FocusService;
import cn.com.tomato.service.TodoService;
import cn.com.tomato.service.TodoSetService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-08-16
 */
@RestController
@RequestMapping("/tomato/todo")
public class TodoController extends BaseController
{

    @Autowired
    private TodoService todoService;

    @Autowired
    private FocusService focusService;

    /**
     * 新增待办和专注
     * 待办一开始时前端生成的,所以需要判断这个待办是否存在,保存在数据库中
     * @param toDoVo
     * @return
     */
    @PostMapping()
    public AjaxResult add(@RequestBody ToDoVo toDoVo){
        ToDo param = new ToDo();
        param.setSid(toDoVo.getSid());
        param.setName(toDoVo.getName());
        List<ToDo> toDos = todoService.selectTodoList(param);
        ToDo result = toDoVo.parse();
        if(toDos== null || toDos.size() == 0) {
            // 没有获取到数据
            todoService.insertTodo(result);
        }else {
            result = toDos.get(0);
        }
        return success(focusService.insertFocus(result.getId(), toDoVo));
    }

    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable(name = "id") Long id){
        return success(todoService.selectTodoById(id));
    }

    @PostMapping("/experience")
    public AjaxResult addExperience(@RequestBody Focus focus){
        logger.info(JSONObject.toJSONString(focus));
        focusService.updateFocus(focus);
        return success();
    }

    @PostMapping("/findByName")
    public AjaxResult findByName(@RequestBody() JSONObject json){
        logger.info(json.toString());
        return success(todoService.selectToDoFocus(json.getString("name")));
    }
}